var data1 = [{'id':'1','operationtype':'修改类型','operationman':'修改人员','operationdate':'time','modificationcontent':'内容','customername':'text1','email':'text2','cucustomerpost':'text3','phone':'text4','mobilephone':'text5','customerwechat':'text6','expressaddress':'text7','customerbirthday':'text8','customerremark':'text9','customerman':'text10'}];

var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,id=getQueryVariable("id")
        ,customercode="",file_count=0;

//表单初始值，修改时调用
if(id!=""){
  /*  form.val("editor-form",{
        "customerabbreviation":"进入查看客户明细",
    })*/
    getlxandlevel();
    init();//加载数据
}
$('#editor-form').find('input,textarea,select').attr('disabled',true);

/*form.val("editor-form",{
    "salesman":salesman
})*/
form.render();
//---客户联系人查询
table.render({
    elem: '#customerselect'
    ,type:"get"
    ,url:"customercontact/selectByPage"
    ,where:{
        "company":customercode
    }
    ,cols: [[
        {field:'name', title:'联系人名称', sort: true,fixed: true}
        ,{field:'email', title:'邮箱地址', sort: true,fixed: true}
        ,{field:'post', title:'职务', sort: true,fixed: true}
        ,{field:'tel', title:'电话', sort: true,fixed: true}
        ,{field:'phone', title:'手机', sort: true,fixed: true}
        ,{field:'wx', title:'微信', sort: true,fixed: true}
        ,{field:'expressAddress', title:'联系人快递地址', sort: true,fixed: true}
        ,{field:'birthday', title:'联系人生日', sort: true,fixed: true}
        ,{field:'remark', title:'联系人备注', sort: true,fixed: true}
        //,{field:'customerman', title:'业务员', sort: true}
    ]]
    ,id: 'customertableid'
    ,page: true
    ,height:300
});

//---日志
table.render({
    elem: '#infoselect'
   /* ,data:data1
    ,cols: [[
        {field:'operationtype', title:'操作类型', sort: true}
        ,{field:'operationman', title:'操作员', sort: true}
        ,{field:'operationdate', title:'操作日期', sort: true}
        ,{field:'modificationcontent', title:'修改内容', sort: true}
    ]]*/
    ,type:"GET"
    ,url:"customerlog/selectByPage"
    ,where:{
        "companycode":customercode
    }
    ,cols: [[
        {field:'type', title:'操作类型', sort: true}
        ,{field:'optioner', title:'操作员', sort: true}
        ,{field:'optionTime', title:'操作日期', sort: true}
        ,{field:'content', title:'修改内容', sort: true}
    ]]
    ,id: 'infoselecttableid'
    ,page: true
    ,height:350
});
$("#shared").on('click',function () {
    layer.open({
        type: 2,
        area: ['1488px', '800px'],
        content: 'views/jsp/BasicData/tpl/shared.jsp'
    });
})
$("#shutdown").on('click',function () {
    parent.detab("views/jsp/BasicData/tpl/viewcustomerinfo.jsp"+(id==''?'':'?id='+id));
})
function init() {
    //获取客户信息
    $.ajax({
        type : 'get',
        //  url : 'resource/getTree',
        data: {"id":id},
        url:'customer/selectByKey',
        dataType:"json",
        // contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                setCustomer(da.data)
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
}

function getFile(code) {
    var obj=[];
    //获取客户信息
    $.ajax({
        type : 'get',
        //  url : 'resource/getTree',
        data: {"code":code},
        url:'wjinfo/getFileByCataLogCode',
        dataType:"json",
        // contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                obj=da.data;
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return obj;
}
function  getSaleman(code) {
    var name="";
    $.ajax({
        type : 'get',
        //  url : 'resource/getTree',
        data: {"code":code},
        url:'customer/selectUserNameByUserCode',
        dataType:"json",
        // contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                name=da.data;

            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return name;
}
//客户信息赋值
function setCustomer(data) {
    var salesman= getSaleman(data.salesman);
    form.val("editor-form",{
        "abbreviation":data.abbreviation,
       // "salesman":salesman,
        "code":data.code,
        "type":data.type,
        "name":data.name,
        "address":data.address,
        "country":data.country,
        "province":data.province,
        "city":data.city,
        "postcode":data.postcode,
        "phone":data.phone,
        "accountPeriod":data.accountPeriod,
        "bank":data.bank,
        "bankaccount":data.bankaccount,
        "level":data.level,
        "mainBusiness":data.mainBusiness,
        "sourceOfCustomers":data.sourceOfCustomers,
        "businessType":data.businessType,
        "homepage":data.homepage,
        "swiftcode":data.swiftcode,
        "followUpPeriod":data.followUpPeriod,
        "expressAddress":data.expressAddress,
        "paymentAccountNo":data.paymentAccountNo,
        "creditLine":data.creditLine,
        "shortCode":data.shortCode,
        "attributesOfCustomers":data.attributesOfCustomers,
    })
    $("input[name='salesman']")[0].value=salesman;
   // $("img#userphotos")[0].src=data.logo;
    if(!data.logo==""){
        $("img#userphotos")[0].src=data.logo;
    }
    customercode=data.code;
    //根据客户编码获取其文件
    var fjdata=  getFile(data.code);

    for(var i = 0;i<fjdata.length;i++){
        file_count ++;
        var filename=fjdata[0].split("filename=")[1];

        var newFile ='<div class="img_tj">'+
            '<div class="file_wdiv"><a id="file_span'+file_count+'" download="'+filename+'" href="'+fjdata[i]+'">'+filename+'</a></div>'+
           /* '<div class="de_div">'+*/
            /* '<input name="file_instructions'+file_count+'" class="layui-input" placeholder="附件说明" value="'+fjdata[i].shuom+'">'+*/
          /*  '<i class="layui-icon layui-icon-reduce-circle" style="font-size: 30px; color: #000000;line-height: 30px;" id="fileUpload_sc_'+file_count+'"></i>'+
            '</div>'+*/
            '</div>';
        $('#addfilediv').append(newFile);
       // uploadFileRender("#fileUpload_"+file_count,"#fileUpload_sc_"+file_count,"#file_span"+file_count);
    }
}
//获取客户类型、等级、跟进阶段
function getlxandlevel() {
    var djs=getdictionary(6,26);//客户等级
    var fls=getdictionary(6,27);//客户类型
    // var jds=getdictionary(6,28);//跟进阶段
    var aoc=getdictionary(1,87);//客户属性
    var country=getdictionary(6,29);//国家/地区
    var province=getdictionary(6,30);//港口/省份
    var city=getdictionary(6,31);//城市
    /*var bank=getdictionary(6,32);//开户银行*/
    var mainBusiness=getdictionary(1,4);//主营产品
    var sourceOfCustomers=getdictionary(1,3);//客户来源
    var businessType=getdictionary(6,33);//业务类型


    var typs=$("select[name='type']");//客户类型
    var level=$("select[name='level']");//客户等级
    // var  status=$("select[name='status']");//跟进阶段
    var  attributesOfCustomers=$("select[name='attributesOfCustomers']");//客户属性
    var countrys=$("select[name='country']");//国家/地区
    var provinces=$("select[name='province']");//港口/省份
    var citys=$("select[name='city']");//城市
    /*var banks=$("select[name='bank']");//开户银行*/
    var mainBusinesss=$("select[name='mainBusiness']");//主营产品
    var businessTypes=$("select[name='businessType']");//业务类型
    var sourceOfCustomerss=$("select[name='sourceOfCustomers']");//客户来源




    level.append(getoption(djs)); //客户等级
    typs.append(getoption(fls)); //客户类型
    // status.append(getoption(jds));//跟进阶段
    attributesOfCustomers.append(getoption(aoc));//客户属性
    countrys.append(getoption(country));//国家/地区
    provinces.append(getoption(province));//港口/省份
    citys.append(getoption(city));//城市
    /*banks.append(getoption(bank));//开户银行*/
    mainBusinesss.append(getoption(mainBusiness));//主营产品
    businessTypes.append(getoption(businessType));//业务类型
    sourceOfCustomerss.append(getoption(sourceOfCustomers));//客户来源

    form.render();

}
function  getoption(objs) {
    var stoptions="";
    for(var i=0;i<objs.length;i++){
        stoptions+="<option value='"+objs[i].code+"'>"+objs[i].name+"</option>";
    }
    return stoptions;
}
$("#print").on("click",function () {
    //window.print();
    window.open("views/jsp/report.jsp?ym=customer&code=code:"+customercode);
});